﻿using XiaoYuStock.Core.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace XiaoYuStock.Core
{
    public class DoctorService
    {
        /// <summary>
        /// 
        /// http://doctor.10jqka.com.cn/600035/
        /// </summary>
        private static string requestUrl = "http://doctor.10jqka.com.cn/{0}/";

        /// <summary>
        /// 描述
        /// </summary>
        private static Regex reg_Overview = new Regex("<p\\s*class=\"content\">.*?</p>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

        /// <summary>
        /// 评分
        /// </summary>
        private static Regex reg_Score = new Regex("<div\\s*class=\"result\">.*?<em>(.*?)</em>.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
        /// <summary>
        /// 股票代码
        /// </summary>
        public string SharesCode { get; set; }


        protected string HtmlContent { get; set; }

        public DoctorData GetDataRange()
        {
            DoctorData model = new DoctorData();
            var url = string.Format(requestUrl, this.SharesCode);
            var htmlContent = RequestHelp.Get(url, Encoding.GetEncoding("GB2312"));
            if (!string.IsNullOrWhiteSpace(htmlContent))
            {
                HtmlContent = htmlContent;
            }
            model = BuildingModel();
            return model;
        }
        private DoctorData BuildingModel()
        {
            try
            {
                DoctorData model = new DoctorData();
                model.OverallScore = ConvertHelper.ToDecimal(RequestHelp.striphtml(Regex.Match(HtmlContent, "<div\\s*class=\"stockvalue\">.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value));
                //
                model.ResultType = Regex.Match(HtmlContent, "<div\\s*class=\"value_bar\">.*?<span\\s*class=\"cur\">(.*?)</span>.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                model.SResult = RequestHelp.striphtml(Regex.Match(HtmlContent, "<div\\s*class=\"value_info\">.*?<li\\s*class=\"short\">(.*?)</li>.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value);
                model.MResult = RequestHelp.striphtml(Regex.Match(HtmlContent, "<div\\s*class=\"value_info\">.*?<li\\s*class=\"mid\">(.*?)</li>.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value);
                model.LResult = RequestHelp.striphtml(Regex.Match(HtmlContent, "<div\\s*class=\"value_info\">.*?<li\\s*class=\"long\">(.*?)</li>.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value);

                model.CapitalPrice = ConvertHelper.ToDecimal(Regex.Match(HtmlContent, "<p\\s*class=\"cnt\">.*?<strong>(\\d+.?\\d+)元</strong>.*?</p>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value);
                model.CapitalDesc = RequestHelp.striphtml(Regex.Match(HtmlContent, "<p\\s*class=\"cnt\">(.*?)</p>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value);
                var date = Regex.Match(HtmlContent, "<span\\s*class='date'>.*?(\\d+)年(\\d+)月(\\d+)日\\s*(\\d+)\\:(\\d+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);

                model.CapitalDate = new DateTime(ConvertHelper.ToInt(date.Groups[1].Value), ConvertHelper.ToInt(date.Groups[2].Value), ConvertHelper.ToInt(date.Groups[3].Value), ConvertHelper.ToInt(date.Groups[4].Value), ConvertHelper.ToInt(date.Groups[5].Value), 0);


                //技术
                var techContent = Regex.Match(HtmlContent, "<div\\s*class=\"box2 indexStat\"\\s*id=\"nav_technical\".*?<div\\s*class=\"box2 indexStat\"\\s*id=\"nav_funds", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;
                model.Technical = BuildingTechnicalModel(techContent);

                //资金
                var capitalContent = Regex.Match(HtmlContent, "<div\\s*class=\"box2 indexStat\"\\s*id=\"nav_funds\".*?<div\\s*class=\"box2 indexStat\"\\s*id=\"nav_message\"", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;
                model.Capital = BuildingCapitalModel(capitalContent);
                //消息
                var msgContent = Regex.Match(HtmlContent, "<div\\s*class=\"box2 indexStat\"\\s*id=\"nav_message\".*?<div\\s*class=\"box2 indexStat\"\\s*id=\"nav_trade\"", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;
                model.Msg = new DoctorDetail();
                model.Msg.Overview = RequestHelp.striphtml(reg_Overview.Match(msgContent).Value);
                model.Msg.Score = ConvertHelper.ToDecimal(reg_Score.Match(msgContent).Groups[1].Value);

                //行业
                var industryContent = Regex.Match(HtmlContent, "<div\\s*class=\"box2 indexStat\"\\s*id=\"nav_trade\".*?<div\\s*class=\"box2 indexStat\"\\s*id=\"nav_basic\"", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;
                model.Industry = new DoctorDetail();
                model.Industry.Overview = RequestHelp.striphtml(reg_Overview.Match(industryContent).Value);
                model.Industry.Score = ConvertHelper.ToDecimal(reg_Score.Match(industryContent).Groups[1].Value);

                //基本面
                var baseContent = Regex.Match(HtmlContent, "<div\\s*class=\"box2 indexStat\"\\s*id=\"nav_basic\".*?<div\\s*class=\"quicknav\">", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;
                model.BaseDoc = BuildingBaseModel(baseContent);
                //<div class="box3 indexStat" stat="f10_nc_11"> <li class="li5 current"></li>
                model.MechanismType = Regex.Match(HtmlContent, "<div\\s*class=\"bd layout2 pr\">.*?<div\\s*class=\"situation\">.*?class=\"li(\\d+) current\"", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
                model.MechanismOverview = Regex.Match(HtmlContent, "<div\\s*class=\"box2wrap basic_score\">.*?<div\\s*class=\"box3 special_border\">.*?<div\\s*class=\"hd2\">(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;

                return model;
            }
            catch (Exception ex)
            {
                LogHelper.ErrorAsync("采集数据出错：" + ex);
                return null;
            }
        }

        private TechnicalDoctor BuildingTechnicalModel(string content)
        {
            TechnicalDoctor model = new TechnicalDoctor();
            model.DKQS = RequestHelp.striphtml(Regex.Match(content, "<div\\s*class=\"box3 special_border\">.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value);
           
          
            model.Overview = RequestHelp.striphtml(reg_Overview.Match(content).Value);
            //
            model.PJCB = ConvertHelper.ToDecimal(Regex.Match(content, "<div\\s*class=\"box3\">.*?<strong>(\\d+.?\\d+).*?</strong>.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value);
            model.PJCBType = Regex.Match(content, "<div\\s*class=\"hd2\">.*?股价在成本(.*?)方.*?", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
            model.SCBX = RequestHelp.striphtml(Regex.Match(content, "<div\\s*class=\"box3 special_border\">.*?<div\\s*class=\"hd2\">(.*?)</div>.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value);
            //
            model.Score = ConvertHelper.ToDecimal(reg_Score.Match(content).Groups[1].Value);
            var xgdxContent = Regex.Match(content, "<div\\s*class=\"techcont\"\\s*>.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value;

            var xgdxMatches = Regex.Matches(xgdxContent, "<dl.*?dlist1.*?</dl>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            if (xgdxMatches != null && xgdxMatches.Count > 0)
            {
                var xgdxGroups = Regex.Matches(xgdxMatches[0].Value, "<dd\\s*tit=\"(.*?)\">.*?</dd>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                List<string> xgdxList = new List<string>();
                if (xgdxGroups != null && xgdxGroups.Count > 0)
                {

                    foreach (Match g in xgdxGroups)
                    {
                        xgdxList.Add(g.Groups[1].Value);
                    }

                }
                model.SGDX = string.Join("|", xgdxList.ToArray());

                var jsxtGroups = Regex.Matches(xgdxMatches[1].Value, "<dd\\s*tit=\"(.*?)\"\\s*info=\"(.*?)\">.*?</dd>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                List<DoctorItem> jsxtList = new List<DoctorItem>();
                if (jsxtGroups != null && jsxtGroups.Count > 0)
                {

                    foreach (Match g in jsxtGroups)
                    {
                        jsxtList.Add(new DoctorItem { Key = g.Groups[1].Value, Value=g.Groups[2].Value });
                    }

                }
                model.JSXT = jsxtList;
                 
            }
           // MRXH
            var mrxhContent = Regex.Match(content, "<div\\s*class=\"techcont\"\\s*>.*?<dl.*?dlist2 clearfix\">(.*?)</dl>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
            var mrxhGroups = Regex.Matches(mrxhContent, "<dd\\s*tit=\"(.*?)\"\\s*info=\"(.*?)\">.*?</dd>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            List<DoctorItem> mrxhList = new List<DoctorItem>();
            if (mrxhGroups != null && mrxhGroups.Count > 0)
            {

                foreach (Match g in mrxhGroups)
                {
                    mrxhList.Add(new DoctorItem { Key = g.Groups[1].Value, Value = g.Groups[2].Value });
                }

            }
            model.MRXH = mrxhList;
            

            //
            model.YLW = ConvertHelper.ToDecimal(Regex.Match(content, "<div\\s*class=\"hd2\">该股压力位为(.*?)元，支撑位为.*?元。</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value);
            model.ZCW = ConvertHelper.ToDecimal(Regex.Match(content, "<div\\s*class=\"hd2\">该股压力位为.*?元，支撑位为(.*?)元。</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value);
            return model;
        }

        

        private CapitalDoctor BuildingCapitalModel(string content)
        {
            CapitalDoctor model = new CapitalDoctor();
            model.ZJLX = RequestHelp.striphtml(Regex.Match(content, "<div\\s*class=\"nx_items\">.*?<div\\s*class=\"hd2\">(.*?)</div>.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value);

            model.Overview = RequestHelp.striphtml(reg_Overview.Match(content).Value);
            //
            model.ZLKP = Regex.Match(content, "<div\\s*class=\"position_bar\" id=\"hotbar\">.*?current\">(.*?)</li>.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
            model.JGCC = RequestHelp.striphtml(Regex.Match(content, "<div\\s*class=\"jgcc_text fl\">.*?</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Value);

            model.Score = ConvertHelper.ToDecimal(reg_Score.Match(content).Groups[1].Value);
         
            return model;
        }


        private BaseDoctor BuildingBaseModel(string content)
        {
            BaseDoctor model = new BaseDoctor();
            model.Overview = RequestHelp.striphtml(reg_Overview.Match(content).Value);
            //
         
            model.YLNL =Regex.Match(content, "<div\\s*class=\"hd2\">盈利能力：(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
          
            model.CZNL = Regex.Match(content, "<div\\s*class=\"hd2\">成长能力：(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;

            model.YYNL = Regex.Match(content, "<div\\s*class=\"hd2\">营运能力：(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;

            model.XJLNL = Regex.Match(content, "<div\\s*class=\"hd2\">现金流能力：(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;

            model.DQCHNL =Regex.Match(content, "<div\\s*class=\"hd2\">短期偿债能力：(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
            model.CQCHNL =Regex.Match(content, "<div\\s*class=\"hd2\">长期偿债能力：(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline).Groups[1].Value;
            model.Score = ConvertHelper.ToDecimal(reg_Score.Match(content).Groups[1].Value);

            //
            var gp = Regex.Matches(content, "<dd.*?info=\"(.*?)\">.*?</dd>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            if (gp != null && gp.Count == 2)
            {
                model.QZGFHNLReason = gp[1].Groups[1].Value;
                model.QZGSZSNLReason = gp[0].Groups[1].Value;

                model.QZGSZSNLType = RequestHelp.striphtml(gp[0].Value);
                model.QZGFHNLType = RequestHelp.striphtml(gp[1].Value);
            }

            return model;
        }
    }
}
